package yingke.com.yingke.ui.left.nearby;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import yingke.com.yingke.R;
import yingke.com.yingke.config.Constant;
import yingke.com.yingke.config.Creator;
import yingke.com.yingke.config.Live;
import yingke.com.yingke.ui.MainActivity;

/**
 * Created by Administrator on 2016/10/20 0020.
 */

public class NearXRecyclerAdapter extends XRecyclerView.Adapter<NearXRecyclerAdapter.MyViewHolder>{
    private Context mContext;
    private List<Live> mList;
    public NearXRecyclerAdapter(Context context){
        mContext =context;
    }
    public void setData(List<Live> list){
        mList =list;
        notifyDataSetChanged();
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.xrecycler_nearby_item, parent, false);
//        DisplayMetrics dm = new DisplayMetrics();
//         //获取屏幕信息
//        ((MainActivity)mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
//
//        int screenWidth = dm.widthPixels;
//
//        int screenHeigh = dm.heightPixels;
//        float scale = mContext.getResources().getDisplayMetrics().density;
//        ViewGroup.LayoutParams params = view.getLayoutParams();
//        params.width=(int)(screenWidth/scale+0.5f)/3;
//        params.height=(int)(screenWidth/scale+0.5f)/3;
//        view.setLayoutParams(params);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Creator creator = mList.get(position).getCreator();
        String imgUrl = Constant.IMG_BASE_URL+creator.getPortrait();
        Glide.with(mContext).load(imgUrl).centerCrop().placeholder(R.drawable.default_head).into(holder.ivImg);
    }

    @Override
    public int getItemCount() {
        return mList!=null?mList.size():0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView ivImg,level;
        TextView tvDistance;
        public MyViewHolder(View itemView) {
            super(itemView);
            level = (ImageView) itemView.findViewById(R.id.iv_level);
            ivImg = (ImageView) itemView.findViewById(R.id.iv_near_img);
            tvDistance = (TextView) itemView.findViewById(R.id.tv_distance);
            DisplayMetrics dm = new DisplayMetrics();
            //获取屏幕信息
            ((MainActivity)mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);

            int screenWidth = dm.widthPixels;

            int screenHeigh = dm.heightPixels;
            float scale = mContext.getResources().getDisplayMetrics().density;
            ViewGroup.LayoutParams params = ivImg.getLayoutParams();
            params.width=(int)(screenWidth/scale+0.5f)/3;
            params.height=(int)(screenWidth/scale+0.5f)/3;
            ivImg.setLayoutParams(params);

        }
    }


}
